import axios from 'axios';
import constants from '../constants/index';
import configs from './common/configs.js';
import { getTokenHeader } from '../../utils/utils';
import {message} from 'antd';
import Qs from 'qs';
const {GET_RECRUIT_INFOR_SUCCESS,GET_FILE_INFOR_SUCCESS,GET_RECRUIT_STEP_SUCCESS,GET_RESULT_FILE_SUCCESS} = constants;
const baseUrl = configs.baseUrl;
const acmUrl=configs.acmUrl;


/**
 * 获取报名信息
 * @param {*} query 
 */
export function getRecruitInfor(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}resume.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type: GET_RECRUIT_INFOR_SUCCESS,
                    data: data.data.pagingList[0],
                    resumeId:data.data.pagingList[0].id
                });
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('报名信息获取失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}

/**
 * 获取附件信息
 * @param {*} query 
 */
export function getFileInfor(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}file/prizeFile.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type: GET_FILE_INFOR_SUCCESS,
                    data: data.data.pagingList,
                });
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('附件信息获取失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}

/**
 * 删除附件信息
 * @param {*} query 
 */
export function deleteFileInfor(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.delete(`${baseUrl}file/prizeFile.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
               message.success('删除成功');
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('附件信息获取失败, 请稍后再试');
            // dispatch({
            //     type: GET_RECRUIT_LIST_FAILURE,
            //     error: new Error('招新列表获取失败, 请稍后再试'),
            // })
        }
    };
}

/**
 * 获取招新进度信息
 * @param {*} query 
 */
export function getSteps(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}queryDetail.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type:GET_RECRUIT_STEP_SUCCESS,
                    data: data.data.pagingList,
                });
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('进度获取失败, 请稍后再试');
        }
    };
}

/**
 * 修改报名信息
 * @param {*} query 
 */
export function changeInfo(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            axios({
                method: 'PUT',
                url: `${baseUrl}resume.do`,
                data: JSON.stringify(query),
                headers: {
                    'Content-Type': 'application/json',
                    ...headers
                    },
                dataType:'json',
            }).then((data)=>{
                if(data.data.code==="SUCCESS"){
                    message.success("修改成功");
                }else if(data.data.msg==='Msg has been examined'){
                    message.warning("信息已审核，无法修改");
                }else{
                    message.warning("修改失败:"+data.data.msg);
                }
            })
        } catch (error) {
            message.error('修改信息失败失败, 请稍后再试');
        }
    };
}

/**
 * 查询成绩文件信息
 * @param {*} query 
 */
export function getResultFile(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}file/scoreFile.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type:GET_RESULT_FILE_SUCCESS,
                    data: data.data,
                });
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('成绩文件获取失败, 请稍后再试');
        }
    };
}

/**
 * 下载成绩文件
 * @param {*} query 
 */
export function getFile(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}file/downloadScore.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
        } catch (error) {
            message.error('下载失败, 请稍后再试');
        }
    };
}